package cn.edu.wit.service.impl;

import java.io.IOException;
import java.util.stream.Collectors;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.edu.wit.db.ArticleRepostitory;
import cn.edu.wit.db.BookRepostitory;
import cn.edu.wit.domain.Article;
import cn.edu.wit.domain.Book;
import cn.edu.wit.service.CrawlerService;

@Service
public class CrawlerServiceImpl implements CrawlerService {

	@Autowired // 让spring把你的对象注入到另一个对象
	private BookRepostitory bookDao;

	@Autowired 
	private ArticleRepostitory articleDao;

	private static final String AGENT = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.109 Safari/537.36";

	private static final String BOOK_URL = "http://dudj.net/hongsejingdian/27/";

	private static final String BASE_URL = "http://dudj.net/";

	@Override
	public void crawlBook() {
		// -链接网站
		try {
			Document document = Jsoup.connect(BOOK_URL).userAgent(AGENT).get();

			// -解析内容
			// Elements elements = document.select("h1");
			// Element element = elements.first();
			// String title = element.text();

			String title = document.select("h1").first().text();

			// Elements elements = document.select("div.neir p");
			// String c1 = elements.get(0).text();
			// String c2 = elements.get(1).text();
			// String description = c1 + c2;

			String description = document.select("div.neir p").stream() /* 计算流 */
					.map(e -> e.text()) /* 把e的值映射成e.text */
					.collect(Collectors.joining("\n"));

			Book book = new Book();
			book.setTitle(title);
			book.setDescription(description);

			bookDao.save(book);

		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	@Override
	public void crawAtricle() {
		try {
			Document document = Jsoup.connect(BOOK_URL).userAgent(AGENT).get();

			Elements elements = document.select(".zlb table:nth-child(2) a");
			elements.forEach(element -> {
				String title = element.attr("title");
				String href = element.attr("href");

				try {
					Document doc = Jsoup.connect(BASE_URL + href).userAgent(AGENT).get();
					
					Elements ps = doc.select("div.zw p");
					
					String content = ps.stream()
							.map(e -> e.text())
							.collect(Collectors.joining("\n"));

					Article article = new Article();
					article.setTitle(title);
					article.setContent(content);

					articleDao.save(article);

				} catch (IOException e) {
					e.printStackTrace();
				}

			});
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

}
